package com.javaear.zblog4java.system.controller.manager;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.javaear.generalmapper.GeneralMapper;
import com.javaear.zblog4java.system.component.AppConfig;
import com.javaear.zblog4java.system.entity.dto.ArticleDTO;
import com.javaear.zblog4java.system.entity.enums.ArticleStatusEnum;
import com.javaear.zblog4java.system.entity.model.ArticleModel;
import com.javaear.zblog4java.system.entity.model.CategoryModel;
import com.javaear.zblog4java.system.entity.model.UserModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author aooer
 */
@Controller
@RequestMapping("system/management")
public class ArticleManagerController {

    @Autowired
    private GeneralMapper generalMapper;

    @RequestMapping("article-management")
    public void query(HttpServletRequest request, ModelMap modelMap) {
        int pageNo = 1;
        String pageNoStr = request.getParameter("pageNo");
        if (StringUtils.isNotBlank(pageNoStr) && StringUtils.isNumeric(pageNoStr))
            pageNo = Integer.parseInt(pageNoStr);
        if (pageNo < 1)
            pageNo = 1;
        Page<ArticleModel> page = new Page<>(pageNo, (int) AppConfig.settingMap.get("systemPageSize"));
        List<ArticleDTO> articles = new ArrayList<>();
        for (ArticleModel m : generalMapper.selectPage(page, new EntityWrapper<>(new ArticleModel()))) {
            ArticleDTO articleDTO = new ArticleDTO();
            BeanUtils.copyProperties(m, articleDTO);
            articleDTO.setAuthName(generalMapper.selectById(m.getUserId(), UserModel.class).getName());
            articleDTO.setCateName(generalMapper.selectById(m.getCateId(), CategoryModel.class).getName());
            articleDTO.setStatusName(ArticleStatusEnum.findDesc(m.getStatus()));
            articles.add(articleDTO);
        }
        modelMap.put("articles", articles);
        modelMap.put("page", page);
        modelMap.put("title", "文章管理");
    }

    /*@RequestMapping("article-add")
    public String add() {
        return managerService.add();
    }

    @RequestMapping("article-edit")
    public String edit() {
        return managerService.edit();
    }

    @RequestMapping("article-remove")
    public String remove() {
        return managerService.remove();
    }*/

}
